package LearnAlgorithm.c_递归and查找排序;

public class p合并两个足够空间有序数组 {
	public static void main(String[] args) {
		int[] arr1 = new int[13]; //{1,3,5,7,9,11,13,15}
		for (int i = 0; i < 8; i++) {
			arr1[i] = i * 2 + 1;
		}
		int[] arr2 = new int[] {2,4,6,8,10};
		mergeArr(arr1, arr2);
		for (int i = 0; i < arr1.length; i++) {
			System.out.print(arr1[i] + " ");
		}
		
	}
	
	public static void mergeArr(int[] arr1, int[] arr2) {
		int current = arr1.length - 1;
		int arr1NumberLast = 0;
		for (int i = 0; i < arr1.length; i++) {
			if (arr1[i] == 0) {
				arr1NumberLast = i - 1;
				break;
			}
		}
		int arr2NumberLast = arr2.length - 1;
		while (current > -1) {
			if (arr1NumberLast < 0) {
				for (int i = arr2NumberLast; i > -1; i--) {
					arr1[current] = arr2[arr2NumberLast];
					current--;
				}
			} else if (arr2NumberLast < 0) {
				break;
			}
			if (arr1[arr1NumberLast] >= arr2[arr2NumberLast]) {
				arr1[current] = arr1[arr1NumberLast];
				arr1NumberLast--;
				current--;
			} else {
				arr1[current] = arr2[arr2NumberLast];
				arr2NumberLast--;
				current--;
			}
		}
	}
}
